from PigeonLabTypes.ImageItem import ImageItem
'''
Created on 20.5.2011

@author: Martin Vegi Kysel
'''

def extractItems(location, session_size = 40, select_seconds = 10 ):
    
    # open the file
    f = open(location,'r');
    
    # session and trial as they are written into the processed list
    # starting with 1
    session = 1;
    trial = 1;
    
    # new list
    extracted_ImageItems = []
    
    for line in f:
            if line.find('STARTER')!=-1:
                # skip the first line
                continue
            
            if line.find('SESSION END')!=-1 or line.find('NEW SESSION')!=-1:
                # skip session end lines
                continue
            
            if len(line.strip())==0:
                # skip empty lines
                continue
            
            #print "---"+line
            
            # if the trial reaches the maximal size of the session
            # reset it and increase the session count
            if ((trial) % (session_size+1) == 0):
                session+=1;
                trial = 1;
            
            # new imageitem
            iI = ImageItem();
            iI.session=session;
            iI.trial =trial;
            #print trial,"/",session
            
            # start parsing the line
            firstPosition = line.find(']')+1
            secondPosition = line.find('[',firstPosition)
            
            name = line[firstPosition:secondPosition].strip()
            iI.image_name = name
            
            
            # classes are explained in PigeonLabTypes.ImageItem
            imageClass = line[secondPosition+1]
            if imageClass=="+":
                iI.itemClass = 1
            elif imageClass=="-":
                iI.itemClass = 2
            else:
                iI.itemClass = 0
            
            # start the parsing of the amount of pecks    
            thirdPosition = line.rfind(']')+1
            
            
            # each part identifies a second
            parts = line[thirdPosition:].split()
            i=0
            sum = 0
            
            # selected seconds (10) and end
            while i<select_seconds :
                if i>=len(parts):
                    break
                
                # ^ identifies multiple zeros
                if parts[i].find("^")!=-1:
                    inner_parts = parts[i].split("^")
                    #print 'there is a ^ at',session,'/',trial
                    i+=int(inner_parts[1])
                    
                    # multiple zeros can overflow the amount of extracted seconds
                    # break
                    if i>select_seconds or i>=len(parts):
                        break
                    parts[i]=inner_parts[0]
                if parts[i].isdigit():
                    sum+=int(parts[i])
                else: print parts[i]+" is not a number"
                #sum+=int(parts[i])
                i+=1
            #print sum
            iI.numPecks = sum
            
            # append to the list and go to the next line
            extracted_ImageItems.append(iI)
            trial+=1
            
            
    return extracted_ImageItems
#=========== eof extractItems ==================
